package com.wechat.wins.http;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

public class SSLContextFactory {
    private static final String KEY_PASS = "password";
    private static final String KEY_FILE = "https.jks";
    private static final String KEY_TYPE = "JKS";
    private static final String KEY_ALGORITHM = "SunX509";
    private static final String SSL_TYPE = "SSL";

    public static SSLContext getSSLContext() throws Exception {
        final char[] password = KEY_PASS.toCharArray();
        final SSLContext sslContext = SSLContext.getInstance(SSL_TYPE);
        final KeyStore keyStore = KeyStore.getInstance(KEY_TYPE);
        final InputStream is = new FileInputStream(ClassLoader.getSystemClassLoader().getResource(KEY_FILE).getFile());
        keyStore.load(is, password);
        final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEY_ALGORITHM);
        kmf.init(keyStore, password);
        sslContext.init(kmf.getKeyManagers(), null, null);
        is.close();
        return sslContext;
    }
}
